import Vue from 'vue'
import App from './App.vue'
import Router from './router'
import store from './store'
import Storage from 'vue-ls'
import 'lib-flexible/flexible'
import 'view-design/dist/styles/iview.css'
import VueLazyLoad from 'vue-lazyload'
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
import VueRouter from 'vue-router';

import lodash from './utils/lodash'
import {
  Input,
  Icon,
  Button,
  Drawer,
  Avatar,
  Page,
  BackTop,
  Form,
  FormItem,
  Message,
  Modal,
  Tabs,
  TabPane,
  Radio,
  RadioGroup,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Select,
  Option,
  Upload,
} from 'view-design'

Vue.component('Input', Input)
Vue.component('Icon', Icon)
Vue.component('Button', Button)
Vue.component('Drawer', Drawer)
Vue.component('Avatar', Avatar)
Vue.component('Page', Page)
Vue.component('BackTop', BackTop)
Vue.component('Form', Form)
Vue.component('FormItem', FormItem)
Vue.component('Modal', Modal)
Vue.component('Tabs', Tabs)
Vue.component('TabPane', TabPane)
Vue.component('Radio', Radio)
Vue.component('RadioGroup', RadioGroup)
Vue.component('Dropdown', Dropdown)
Vue.component('DropdownMenu', DropdownMenu)
Vue.component('DropdownItem', DropdownItem)
Vue.component('Select', Select)
Vue.component('Option', Option)
Vue.component('Upload', Upload)

Vue.use(VueLazyLoad, {
  error: require('./assets/images/logo.png'),
  loading: require('./assets/images/logo.png')
})

Vue.use(mavonEditor)
Vue.use(lodash)

Vue.prototype.$Message = Message
Vue.config.productionTip = false

Vue.use(Storage, {
  namespace: 'boblog__',
  name: 'ls',
  storage: 'local'
})

// 路由配置
const RouterConfig = {
  mode: 'history',
  routes: Router,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
};
const router = new VueRouter(RouterConfig);

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.PUBLIC_PATH,
//   routes,
//   scrollBehavior (to, from, savedPosition) {
//     // 兼容
//     if (savedPosition) {
//       return savedPosition
//     } else {
//       return {
//         x: 0,
//         y: 0
//       }
//     }
//   }
// })
router.beforeEach(async (to, from, next) => {
  if (to.fullPath.includes('/login'))  return next()
  // let token = localStorage.token
	if (!localStorage.token) {
    Message.error('权限未授权')
		return next({ name: 'login', replace: true })
	}
  next()
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
